module planegame{

    export class StageManager extends egret.DisplayObjectContainer {

        private static _instance    :StageManager;

        private startMenu           :StartMenu;
        private game                :Game;

        // not use
        private currentStage        :egret.DisplayObjectContainer;

        private webSocket:egret.WebSocket;
        
        private constructor() {
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }

        private onAddToStage() {
            this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

            let stageW = this.stage.stageWidth;
            let stageH = this.stage.stageHeight;

            /* webSocket
            this.webSocket = new egret.WebSocket();        
            this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);                            
            this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);    
            this.webSocket.connectByUrl("ws://localhost:8080/webSocketTest/websocket/snake1");
            */

            // 初始化各个场景 由于场景较少 保留了缓存
            this.startMenu = new StartMenu(stageW, stageH);
            this.game = new Game(stageW, stageH);
            this.addChild(this.getStageStartMenu());
        }

        /* websocket
        private onSocketOpen():void {    
            var cmd = "Hello Egret WebSocket";    
            console.log("连接成功，发送数据：" + cmd);
            this.webSocket.writeUTF(cmd);
        }
        private onReceiveMessage(e:egret.Event):void {    
            var msg = this.webSocket.readUTF();    
            console.log("收到数据：" + msg);
        }
        */

        public static instance():StageManager {
            if(!this._instance) {
                this._instance = new StageManager();
            }
            return this._instance;
        }

        public getStageStartMenu():StartMenu {
            if(!this.startMenu.hasEventListener("changeStageGame")){
                this.startMenu.addEventListener("changeStageGame", this.changeStageGame, this);
            }
            return this.startMenu;
        }

        public getStageGame():Game {
            if(!this.game.hasEventListener("changeStageStartMenu")){
                this.game.addEventListener("changeStageStartMenu", this.changeStageStartMenu, this);
            }
            return this.game;
        }

        public changeStageGame(evt:egret.Event) {
            egret.log("changeStageGame");
            this.getStageStartMenu().end();
            if(this.numChildren > 0) {
                this.removeChildren();
            }
            this.addChild(this.getStageGame());
        }

        public changeStageStartMenu(evt:egret.Event) {
            egret.log("changeStageStartMenu");
            this.getStageGame().end();
            if(this.numChildren > 0) {
                this.removeChildren();
            }
            this.addChild(this.getStageStartMenu());
        }

    }
}